def read_array_from_file(file_path):
    try:
        with open(file_path, 'r') as file:
            content = file.read().strip()
            if not content:
                return []
            arr = list(map(int, content.split()))
            return arr[:100]  # 确保元素个数不超过100个
    except FileNotFoundError:
        return []

def find_max_circular_subarray_sum(arr):
    if not arr:
        return 0, -1, -1

    n = len(arr)
    max_kadane, start_kadane, end_kadane = kadane_algorithm(arr)

    if all(num <= 0 for num in arr):
        return max_kadane, start_kadane, end_kadane

    total_sum = sum(arr)
    modified_arr = [-num for num in arr]
    max_wrap, start_wrap, end_wrap = kadane_algorithm(modified_arr)

    max_wrap = total_sum + max_wrap

    if max_kadane > max_wrap:
        return max_kadane, start_kadane, end_kadane
    else:
        # 调整环形子数组的起始和结束下标
        new_start = (end_wrap + 1) % n
        new_end = (start_wrap-1) % n
        return max_wrap, new_start, new_end

def kadane_algorithm(arr):
    max_current = max_global = arr[0]
    start_global = start_current = end_global = 0

    for i in range(1, len(arr)):
        if arr[i] > max_current + arr[i]:
            max_current = arr[i]
            start_current = i
        else:
            max_current += arr[i]

        if max_current > max_global:
            max_global = max_current
            start_global = start_current
            end_global = i

    return max_global, start_global, end_global

# 主函数，用于演示
def main():
    file_path = "array.txt"
    arr = read_array_from_file(file_path)
    max_sum, start, end = find_max_circular_subarray_sum(arr)
    print(f"最大子数组和：{max_sum}")
    print(f"起始下标：{start}，结束下标：{end}")

if __name__ == "__main__":
    main()